
#pragma once
#include <vsg/all.h>

namespace qbe::input {

class MouseHandle : public vsg::Inherit<vsg::Visitor, MouseHandle> {
 public:
  void apply(vsg::MoveEvent& e) override {
    // SPDLOG_INFO("MoveEvent");
    mouse_move = true;
    move_event = &e;
  }

  void apply(vsg::ButtonPressEvent& e) override {
    mouse_press = true;
    button_press_event = &e;
  }
  void apply(vsg::ButtonReleaseEvent& e) override {
    mouse_release = true;
    button_release_event = &e;
  }

  void apply(vsg::ScrollWheelEvent& e) override {
    scroll_wheel = true;
    scroll_wheel_event = &e;
  }

  void apply(vsg::ConfigureWindowEvent& e) override {
    configure_window = true;
    configure_window_event = &e;
  };

  bool mouseMoved(int32_t* x, int32_t* y) {
    if (mouse_move) {
      *x = move_event->x;
      *y = move_event->y;
      // SPDLOG_INFO("mouse: {}, {}", *x, *y);
      return true;
    }
    return false;
  }

  bool mousePressed(vsg::ButtonMask mask) {
    if (mouse_press) {
      return mask == button_press_event->mask;
    }
    return false;
  }

  bool mouseReleased(vsg::ButtonMask mask) {
    if (mouse_release) {
      return mask == button_release_event->mask;
    }
    return false;
  }

  bool mouseReleased(uint32_t button) {
    if (mouse_release) {
      return button == button_release_event->button;
    }
    return false;
  }

  bool scrollWheel(int* delta_y) {
    if (scroll_wheel) {
      *delta_y = scroll_wheel_event->delta.y;
      return true;
    }
    return false;
  }

  bool resized(uint32_t* x, uint32_t* y, uint32_t* width, uint32_t* height) {
    if (configure_window) {
      *x = configure_window_event->x;
      *y = configure_window_event->y;
      *width = configure_window_event->width + 16;
      *height = configure_window_event->height + 39;
      return true;
    }
    return false;
  }

  void reset() {
    mouse_move = false;
    mouse_press = false;
    mouse_release = false;
    scroll_wheel = false;
    configure_window = false;
  };

  bool mouse_move{false};
  vsg::MoveEvent* move_event{nullptr};

  bool mouse_press{false};
  vsg::ButtonPressEvent* button_press_event{nullptr};

  bool mouse_release{false};
  vsg::ButtonReleaseEvent* button_release_event{nullptr};

  bool scroll_wheel{false};
  vsg::ScrollWheelEvent* scroll_wheel_event{nullptr};

  bool configure_window{false};
  vsg::ConfigureWindowEvent* configure_window_event{nullptr};
};

}  // namespace qbe::input